import logging

import allure
from appium.webdriver.common.appiumby import AppiumBy

from base.base_page import BasePage


class SettingPage(BasePage):

    _wifi_locator_text = "WiFi"
    _connected_wifi = (AppiumBy.XPATH, "//*[@text='已连接']")
    _set_proxy_locator = (AppiumBy.XPATH, "//*[@text='设置代理']")
    _set_proxy_manual_locator = (AppiumBy.XPATH, "//*[@text='手动']")
    _set_proxy_none_locator = (AppiumBy.XPATH, "//*[@text='无']")
    _delete_wifi_locator = (AppiumBy.XPATH, "//*[@text='删除此网络']")
    _delete_wifi_cancel_locator = (AppiumBy.XPATH, "//*[@text='取消']")
    _delete_wifi_confirm_locator = (AppiumBy.XPATH, "//*[@text='删除']")
    _wifi_switch_locator = (AppiumBy.XPATH, "//*[@resource-id='com.pi.pilot.setting:id/tv_name'][@text='WiFi']")
    _available_wifi_locator = (AppiumBy.XPATH, "//*[@text='可用WiFi']")
    _PI_wifi_locator_text = "PI"
    _wifi_password_input_box = (AppiumBy.XPATH, "//*[@resource-id='com.pi.pilot.setting:id/et']")
    _PI_wifi_password = "piis3.14"
    _wifi_connect_locator = (AppiumBy.XPATH, "//*[@text='连接']")
    _wifi_connect_success_locator = (AppiumBy.XPATH, "//*[@resource-id='com.pi.pilot.setting:id/tv_message']")
    _wifi_ip_locator = (AppiumBy.XPATH, "//*[@resource-id='com.pi.pilot.setting:id/tv_ip']")

    _bluetooth_locator_text = "蓝牙"
    _bluetooth_switch_locator = (AppiumBy.XPATH, "//*[@resource-id='com.pi.pilot.setting:id/tv_name'][@text='蓝牙']")

    _location_locator_text = "定位"
    _GPS_switch_locator = (AppiumBy.XPATH, "//*[@text='GPS']")
    _coordinate_system_auto_locator = (AppiumBy.XPATH, "//*[@text='自动']")
    _coordinate_system_WGS84_locator = (AppiumBy.XPATH, "//*[@text='只用WGS-84']")

    _hotspot_locator_text = "热点"
    _hotspot_switch_locator = (AppiumBy.XPATH, "//*[@resource-id='com.pi.pilot.setting:id/tv_name'][@text='热点']")
    _hotspot_setting_locator = (AppiumBy.XPATH, "//*[@text='热点设置']")
    _hotspot_name_locator = (AppiumBy.XPATH, "//*[@resource-id='com.pi.pilot.setting:id/tv_hotspot_name']")
    _hotspot_password_locator = (AppiumBy.XPATH, "//*[@resource-id='com.pi.pilot.setting:id/tv_hotspot_password']")

    _ethernet_locator_text = "有线网"
    _ethernet_ip_settings_locator = (AppiumBy.XPATH, "//*[@text='IP设置']")
    _ethernet_ip_auto_locator = (AppiumBy.XPATH, "//*[@text='自动获取']")
    _ethernet_ip_manual_locator = (AppiumBy.XPATH, "//*[@text='手动配置']")

    _nadir_logo_locator_text = "底部 Logo"
    _nadir_logo_switch_locator = (AppiumBy.XPATH, "//*[@resource-id='com.pi.pilot.setting:id/tv_name'][@text='底部 Logo']")
    _logo_size_locator_center_coordinate = [(240, 452)]
    _logo_style_1_locator = (AppiumBy.XPATH, "//*[@resource-id='com.pi.pilot.setting:id/rv_available_logos']/"
                                             "android.view.ViewGroup[1]")
    _logo_style_2_locator = (AppiumBy.XPATH, "//*[@resource-id='com.pi.pilot.setting:id/rv_available_logos']/"
                                             "android.view.ViewGroup[2]")

    _date_time_locator_text = "日期与时间"
    _date_time_auto_switch_locator = (AppiumBy.XPATH, "//*[@text='自动设置']")
    _date_locator = (AppiumBy.XPATH, "//*[@text='日期']")
    _time_locator = (AppiumBy.XPATH, "//*[@text='时间']")
    _timezone_locator = (AppiumBy.XPATH, "//*[@text='时区']")
    _24hours_locator = (AppiumBy.XPATH, "//*[@text='24小时制']")
    _date_time_confirm_locator = (AppiumBy.XPATH, "//*[@text='确认']")

    _screen_brightness_locator_text = "屏幕与亮度"
    _brightness_locator_center_coordinate = [(240, 199)]
    _screen_sleep_locator = (AppiumBy.XPATH, "//*[@text='定时息屏']")
    _screen_sleep_30s_locator = (AppiumBy.XPATH, "//*[@text='30秒']")
    _screen_sleep_1min_locator = (AppiumBy.XPATH, "//*[@text='1分钟']")
    _screen_sleep_3min_locator = (AppiumBy.XPATH, "//*[@text='3分钟']")
    _screen_sleep_5min_locator = (AppiumBy.XPATH, "//*[@text='5分钟']")
    _screen_sleep_10min_locator = (AppiumBy.XPATH, "//*[@text='10分钟']")
    _screen_sleep_never_locator = (AppiumBy.XPATH, "//*[@text='永不']")
    _led_photo_locator = (AppiumBy.XPATH, "//*[@text='拍照']")
    _led_video_locator = (AppiumBy.XPATH, "//*[@text='录像']")
    _led_live_locator = (AppiumBy.XPATH, "//*[@text='直播']")

    _sound_locator_text = "声音"
    _speaker_locator_center_coordinate = [(240, 199)]
    _photo_shutter_locator = (AppiumBy.XPATH, "//*[@text='拍照快门声']")
    _video_shutter_locator = (AppiumBy.XPATH, "//*[@text='录像快门声']")
    _live_shutter_locator = (AppiumBy.XPATH, "//*[@text='直播快门声']")
    _low_battery_locator = (AppiumBy.XPATH, "//*[@text='低电量提示音']")

    _iq_codec_locator_text = "画质与编码"
    _iq_locator = (AppiumBy.XPATH, "//*[@text='画质']")
    _iq_standard_locator = (AppiumBy.XPATH, "//*[@text='标准画质']")
    _iq_high_locator = (AppiumBy.XPATH, "//*[@text='高画质']")
    _codec_locator = (AppiumBy.XPATH, "//*[@text='编码方式']")
    _codec_H_264_locator = (AppiumBy.XPATH, "//*[@text='H.264']")
    _codec_H_265_locator = (AppiumBy.XPATH, "//*[@text='H.265']")

    _anti_flicker_locator_text = "抗频闪"
    _50hz_locator = (AppiumBy.XPATH, "//*[@text='50Hz']")
    _60hz_locator = (AppiumBy.XPATH, "//*[@text='60Hz']")
    _auto_locator = (AppiumBy.XPATH, "//*[@text='Auto']")
    # _off_locator = (AppiumBy.XPATH, "//*[@text='关闭']")  # 已在7.1.2版本移除此选项

    _language_locator_text = "语言"
    _language_english_locator = (AppiumBy.XPATH, "//*[@text='English']")
    _language_chinese_locator = (AppiumBy.XPATH, "//*[@text='简体中文']")

    _keyboard_locator_text = "键盘"
    _panox_keyboard_locator = (AppiumBy.XPATH, "//*[@text='PanoX输入法']")
    _native_keyboard_locator = (AppiumBy.XPATH, "//*[@text='原生输入法']")

    _storage_locator = (AppiumBy.XPATH, "//*[@text='存储']")
    _storage_locator_text = "存储"
    _init_tf_locator = (AppiumBy.XPATH, "//*[@text='格式化并清空所有文件']")
    _confirm_init_tf_locator = (AppiumBy.XPATH, "//*[@text='全部删除']")
    _tf_pulled_out_message = (AppiumBy.XPATH, "//*[@text='已拔出TF卡。']")

    _energy_saver_locator_text = "节能"
    _auto_power_Off_locator = (AppiumBy.XPATH, "//*[@text='自动关机']")
    _auto_power_Off_switch_locator = (AppiumBy.XPATH, "//*[@resource-id='com.pi.pilot.setting:id/tv_name'][@text='自动关机']")
    _auto_power_Off_10min_locator = (AppiumBy.XPATH, "//*[@text='10分钟']")
    _auto_power_Off_30min_locator = (AppiumBy.XPATH, "//*[@text='30分钟']")
    _auto_power_Off_1h_locator = (AppiumBy.XPATH, "//*[@text='1小时']")
    _auto_power_Off_2h_locator = (AppiumBy.XPATH, "//*[@text='2小时']")

    _system_update_locator_text = "系统更新"

    _reset_locator_text = "还原"

    _about_locator_text = "关于本机"
    _device_name_locator = (AppiumBy.XPATH, "//*[@text='本机名称']")
    _legal_info_locator = (AppiumBy.XPATH, "//*[@text='法律信息']")
    _opensource_licenses_locator = (AppiumBy.XPATH, "//*[@text='开放源代码许可']")
    _user_agreement_locator = (AppiumBy.XPATH, "//*[@text='用户协议']")
    _privacy_policy_locator = (AppiumBy.XPATH, "//*[@text='隐私政策']")
    _warranty_policy_locator = (AppiumBy.XPATH, "//*[@text='保修政策']")
    _regulatory_certification = (AppiumBy.XPATH, "//*[@text='法规认证']")

    def init_tf(self):
        self.swipe_find_text_click(self._storage_locator_text)
        self.find_and_click(self._init_tf_locator)
        self.find_and_click(self._confirm_init_tf_locator)
        self.wait_until_element_exists(self._tf_pulled_out_message)
        self.click_blank_up(2)
        return self

    def connect_pi_wifi(self):
        self.wait_for_one_second(5)
        if self.is_element_exist(self._connected_wifi):
            pass
        else:
            self.swipe_find_text_click(self._PI_wifi_locator_text)
            self.find_and_send_text(self._wifi_password_input_box, self._PI_wifi_password)
            self.back()
            self.find_and_click(self._wifi_connect_locator)
            self.wait_until_element_exists(self._wifi_connect_success_locator)
            self.click_blank_center()
            return self

    def confirm_wifi_connected(self):
        self.swipe_find_text_click(self._wifi_locator_text)
        self.wait_for_one_second(7)
        if self.is_element_exist(self._connected_wifi):
            logging.info("WiFi已连接")
        elif self.is_element_exist(self._available_wifi_locator):
            logging.info("WiFi已开启但未连接，尝试连接PI WiFi")
            self.connect_pi_wifi()
        else:
            self.find_and_click(self._wifi_switch_locator)
            self.connect_pi_wifi()
            return self

    def iterate_wifi(self):
        self.confirm_wifi_connected()
        self.swipe_find_text_click(self._wifi_locator_text)
        self.find_and_click(self._connected_wifi)
        self.find_and_click(self._set_proxy_locator)
        self.find_and_click(self._set_proxy_manual_locator)
        self.find_and_click(self._set_proxy_none_locator)
        self.back()
        self.find_and_click(self._delete_wifi_locator)
        self.find_and_click(self._delete_wifi_confirm_locator)
        self.confirm_wifi_connected()

    def iterate_bluetooth(self):
        self.swipe_find_text_click(self._bluetooth_locator_text)
        self.find_and_click(self._bluetooth_switch_locator)
        self.wait_for_one_second(5)
        self.find_and_click(self._bluetooth_switch_locator)

    def iterate_location(self):
        self.swipe_find_text_click(self._location_locator_text)
        self.find_and_click(self._GPS_switch_locator)
        self.find_and_click(self._GPS_switch_locator)
        self.find_and_click(self._coordinate_system_auto_locator)
        self.find_and_click(self._coordinate_system_WGS84_locator)
        self.find_and_click(self._coordinate_system_auto_locator)

    def iterate_hotspot(self):
        self.swipe_find_text_click(self._hotspot_locator_text)
        self.find_and_click(self._hotspot_switch_locator)
        self.find_and_click(self._hotspot_setting_locator)
        self.back()
        self.find_and_click(self._hotspot_switch_locator)

    def iterate_ethernet(self):
        self.swipe_find_text_click(self._ethernet_locator_text)
        self.find_and_click(self._ethernet_ip_settings_locator)
        self.find_and_click(self._ethernet_ip_manual_locator)
        self.find_and_click(self._ethernet_ip_auto_locator)

    def iterate_nadir_logo(self):
        self.swipe_find_text_click(self._nadir_logo_locator_text)
        self.find_and_click(self._nadir_logo_switch_locator)
        self.find_and_click(self._nadir_logo_switch_locator)
        self.find_coordinate_and_click(self._logo_size_locator_center_coordinate)
        self.find_and_click(self._logo_style_2_locator)
        self.find_and_click(self._logo_style_1_locator)

    def iterate_date_time(self):
        self.swipe_find_text_click(self._date_time_locator_text)
        self.find_and_click(self._date_time_auto_switch_locator)
        self.find_and_click(self._date_locator)
        self.find_and_click(self._date_time_confirm_locator)
        self.find_and_click(self._time_locator)
        self.find_and_click(self._date_time_confirm_locator)
        self.find_and_click(self._timezone_locator)
        self.find_and_click(self._date_time_confirm_locator)
        self.find_and_click(self._24hours_locator)
        self.find_and_click(self._24hours_locator)
        self.find_and_click(self._date_time_auto_switch_locator)

    def iterate_screen_brightness(self):
        self.swipe_find_text_click(self._screen_brightness_locator_text)
        self.find_coordinate_and_click(self._brightness_locator_center_coordinate)
        self.find_and_click(self._screen_sleep_locator)
        self.find_and_click(self._screen_sleep_1min_locator)
        self.find_and_click(self._screen_sleep_3min_locator)
        self.find_and_click(self._screen_sleep_5min_locator)
        self.find_and_click(self._screen_sleep_10min_locator)
        self.find_and_click(self._screen_sleep_never_locator)
        self.find_and_click(self._screen_sleep_30s_locator)
        self.back()
        self.find_and_click(self._led_photo_locator)
        self.find_and_click(self._led_photo_locator)
        self.find_and_click(self._led_video_locator)
        self.find_and_click(self._led_video_locator)
        self.find_and_click(self._led_live_locator)
        self.find_and_click(self._led_live_locator)

    def iterate_sound(self):
        self.swipe_find_text_click(self._sound_locator_text)
        self.find_coordinate_and_click(self._speaker_locator_center_coordinate)
        self.find_and_click(self._photo_shutter_locator)
        self.find_and_click(self._photo_shutter_locator)
        self.find_and_click(self._video_shutter_locator)
        self.find_and_click(self._video_shutter_locator)
        self.find_and_click(self._live_shutter_locator)
        self.find_and_click(self._live_shutter_locator)
        self.find_and_click(self._low_battery_locator)
        self.find_and_click(self._low_battery_locator)

    def iterate_iq_codec(self):
        self.swipe_find_text_click(self._iq_codec_locator_text)
        self.find_and_click(self._iq_locator)
        self.find_and_click(self._iq_high_locator)
        self.find_and_click(self._iq_standard_locator)
        self.back()
        self.find_and_click(self._codec_locator)
        self.find_and_click(self._codec_H_264_locator)
        self.find_and_click(self._codec_H_265_locator)

    def iterate_anti_flicker(self):
        self.swipe_find_text_click(self._anti_flicker_locator_text)
        self.find_and_click(self._50hz_locator)
        self.find_and_click(self._60hz_locator)
        self.find_and_click(self._auto_locator)

    def iterate_language(self):
        self.swipe_find_text_click(self._language_locator_text)
        self.find_and_click(self._language_english_locator)
        self.find_and_click(self._language_chinese_locator)

    def iterate_keyboard(self):
        self.swipe_find_text_click(self._keyboard_locator_text)
        self.find_and_click(self._native_keyboard_locator)
        self.find_and_click(self._panox_keyboard_locator)

    def iterate_storage(self):
        self.init_tf()

    def iterate_energy_saver(self):
        self.swipe_find_text_click(self._energy_saver_locator_text)
        self.find_and_click(self._auto_power_Off_locator)
        self.wait_for_one_second()
        self.find_and_click(self._auto_power_Off_switch_locator)
        self.find_and_click(self._auto_power_Off_switch_locator)
        self.find_and_click(self._auto_power_Off_10min_locator)
        self.find_and_click(self._auto_power_Off_1h_locator)
        self.find_and_click(self._auto_power_Off_2h_locator)
        self.find_and_click(self._auto_power_Off_30min_locator)

    def iterate_system_update(self):
        self.swipe_find_text_click(self._system_update_locator_text)

    def iterate_reset(self):
        self.swipe_find_text_click(self._reset_locator_text)

    def iterate_about(self):
        self.swipe_find_text_click(self._about_locator_text)
        self.find_and_click(self._device_name_locator)
        self.back()
        self.find_and_click(self._legal_info_locator)
        self.find_and_click(self._opensource_licenses_locator)
        self.back()
        self.find_and_click(self._user_agreement_locator)
        self.back()
        self.find_and_click(self._privacy_policy_locator)
        self.back(2)
        self.find_and_click(self._warranty_policy_locator)
        self.back()
        self.find_and_click(self._regulatory_certification)

